﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NovoUI
{
    public partial class Form_SetIICDevice : Form
    {

        public Form_SetIICDevice(int _devID, int _totalDevCnt, string _devName, byte _devAddr, uint _i2cMode, byte _puEn)
        {
            InitializeComponent();
            
            this.cmb_ChooseIICDev.Items.Clear();
            for(int ix = 0; ix < _totalDevCnt; ix++) 
            {
                this.cmb_ChooseIICDev.Items.Add(String.Format("{0} - {1}", _devName, ix));
            }

            if (_totalDevCnt > 0 && (_devID < _totalDevCnt - 1))
            {
                this.cmb_ChooseIICDev.SelectedIndex = _devID;
            }

            this.numUD_SetDevAddr.Value = _devAddr;
            this.cmb_I2CMode.SelectedIndex = (int)_i2cMode;
            this.rdb_EnablePU.Checked = (_puEn == 1) ? true : false;
        }

        // Form closing event to make Main form to update device configuration
        public delegate void SetIICDevClosingEventHandler(object sender, FormClosingEventArgs e);
        public event SetIICDevClosingEventHandler FormClosingEvent;

        private int devIndex = 0;
        public int SelectedDevIndex
        {
            get { return devIndex; }
        }

        private int totalDevCount = 0;

        private byte devAddr = 0x42;
        public byte DevAdd
        {
            get { return devAddr; }
        }

        private uint i2cMode = 1;
        public uint I2CMode
        {
            get { return i2cMode; }
        }

        private byte pu_en = 0;
        public byte PU_EN
        {
            get { return pu_en; }
        }

        private void Form_SetIICDevice_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(FormClosingEvent != null)
                FormClosingEvent(this, e);
        }

        private void cmb_ChooseIICDev_SelectedIndexChanged(object sender, EventArgs e)
        {
            devIndex = this.cmb_ChooseIICDev.SelectedIndex;
        }

        private void numUD_SetDevAddr_ValueChanged(object sender, EventArgs e)
        {
            this.devAddr = (byte)this.numUD_SetDevAddr.Value;
        }

        private void cmb_I2CMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.i2cMode = (uint)this.cmb_I2CMode.SelectedIndex;
        }

        private void rdb_EnablePU_CheckedChanged(object sender, EventArgs e)
        {
            this.pu_en = (byte)(this.rdb_EnablePU.Checked ? 1 : 0);
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form_SetIICDevice_Load(object sender, EventArgs e)
        {

        }
    }
}
